﻿    using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using MySql.Data.MySqlClient;

namespace Plazos
{
    class DBConnect
    {
        protected MySqlConnection connection;
        private string server;
        private string database;
        private string uid;
        private string password;
        private string port;

        // Método constructor, crea la conexión con la base de datos.
        public DBConnect()
        {
            server = "db4free.net";
            database = "plazos" ;
            uid = "fundacionupla";
            password = "fundacionupla2015";
            port = "3306";

            string connectionString;
            
            connectionString = "Server=" + server + "; "
                + "Port=" + port + "; "
                + "Database=" + database + "; " 
                + "Uid=" + uid + "; " 
                + "Pwd=" + password + ";" ;
            
            Console.WriteLine(connectionString);
            connection = new MySqlConnection(connectionString);

        }

        // Abre la conexión con la base de datos
        public bool OpenConnection()
        {
            try
            {
                connection.Open();
                Console.WriteLine("Conectado con éxito");
                return true;
            }
            catch(MySqlException e)
            {
                Console.WriteLine("No pudo conectar a la base de datos.");
                Console.WriteLine(e.Message);
                Console.WriteLine(e.StackTrace);
                return false;
            }
        }

        // Cierra la conexión con la base de datos.
        public bool CloseConnection()
        {
            try
            {
                connection.Close();
                return true;
            }
            catch (MySqlException e)
            {
                Console.WriteLine(e.Message);
                return false;
            }
        }

    }
}
